<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/18 9:37
 */

namespace app\admin\controller;


use think\facade\View;
use app\admin\validate\Role as RoleValidate;
use app\admin\business\Role as BusRole;

class Role extends AdminBase
{

    /**
     * 功能说明 菜单栏首页
     * @author: wang
     * @Date: 2020/3/23 22:28
     * @return string|\think\response\Json
     * @throws \Exception
     */
    public function index()
    {
        $BusRole = new BusRole();
        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit', '', 'trim,htmlspecialchars')??15;
            $name = $this->request->param('name', '', 'trim,htmlspecialchars');
            $where = [];
            if (!empty($name) && $name != '-1') {
                $where[] = ['id', '=', $name];
            }
            $list = $BusRole->getRoleList($where, $limit);
            return $this->show(config('status.lay_table'), 'success', $list);
        }
        $data = $BusRole->getAllRole();
        return View::fetch('index', [
            'data' => $data
        ]);
    }

    /**
     * 功能说明 添加菜单栏
     * @author: wang
     * @Date: 2020/3/23 22:28
     * @return string|\think\response\Json
     * @throws \Exception
     */
    public function add()
    {

        if ($this->request->isPost()) {
            $name = $this->request->param('name', '', 'trim,htmlspecialchars');
            $status = $this->request->param('status', '', 'trim,htmlspecialchars');
            $data = [
                'name' => $name,
                'status' => $status,
            ];
            $AuthValidate = new RoleValidate();
            if (!$AuthValidate->scene('add')->check($data)) {
                return $this->show(config('status.error'), $AuthValidate->getError());
            }

            $BusAuth = new BusRole();
            try {
                $result = $BusAuth::add($data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }

            if ($result == true) {
                return $this->result(config('status.success'), '添加成功', '/admin/role/index');
            } else {
                return $this->show(config('status.error'), '添加失败');
            }
        }

        return View::fetch('save');
    }

    /**
     * 功能说明 通过ID更新角色
     * @author: wang
     * @Date: 2020/3/24 18:06
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $id = $this->request->param('id', '', 'trim,htmlspecialchars');
        $BusRole = new BusRole();
        if ($this->request->isPost()) {
            $status = $this->request->param('status', '', 'trim,htmlspecialchars');
            $name = $this->request->param('name', '', 'trim,htmlspecialchars');

            $data = [
                'id' => $id,
                'status' => $status,
                'name' => $name,
            ];
            $RoleValidate = new RoleValidate();
            if (!$RoleValidate->scene('edit')->check($data)) {
                return $this->show(config('status.error'), $RoleValidate->getError());
            }

            try {
                $result = $BusRole::edit($data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }

            if ($result == true) {
                return $this->result(config('status.success'), '更新成功', '/admin/role/index');
            } else {
                return $this->show(config('status.error'), '更新失败');
            }
        }

        $data = $BusRole::getDataById($id);
        return View::fetch('save', [
            'data' => $data,
        ]);
    }

    /**
     * 功能说明 删除数据
     * @author: wang
     * @Date: 2020/3/25 20:50
     * @return \think\response\Json
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $type = $this->request->param('type', '', 'trim,htmlspecialchars');
            if (!empty($type) && $type == 'all-del') {
                $ids = $this->request->param('ids', '', 'trim,htmlspecialchars');
                $BusRole = new BusRole();
                try {
                    $result = $BusRole::delDataByIds($ids);
                } catch (\Exception $e) {
                    return $this->show(config('status.error'), $e->getMessage());
                }
                if ($result !== false) {
                    return $this->result(config('status.success'), '删除成功', '/admin/role/index');
                } else {
                    return $this->show(config('status.error'), '删除失败');
                }
            } else {
                $id = $this->request->param('id', '', 'trim,htmlspecialchars');
                $data = [
                    'id' => $id,
                ];
                $RoleValidate = new RoleValidate();
                if (!$RoleValidate->scene('del')->check($data)) {
                    return $this->show(config('status.error'), $RoleValidate->getError());
                }
                $BusRole = new BusRole();
                try {
                    $result = $BusRole::delDataById($id);
                } catch (\Exception $e) {
                    return $this->show(config('status.error'), $e->getMessage());
                }

                if ($result !== false) {
                    return $this->result(config('status.success'), '删除成功', '/admin/role/index');
                } else {
                    return $this->show(config('status.error'), '删除失败');
                }
            }

        }
    }


    /**
     * 功能说明 更新状态
     * @author: wang
     * @Date: 2020/3/25 18:10
     * @return \think\response\Json
     */
    public function upStatus()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', '', 'trim,htmlspecialchars');
            $status = $this->request->param('status', '', 'trim,htmlspecialchars');
            if (!empty($id) && !empty($status) && ($status == 'on' || $status == 'off')) {
                if ($status == 'on') {
                    $status = 1;
                } elseif ($status == 'off') {
                    $status = 0;
                }

                $RoleValidate = new RoleValidate();
                $data = [
                    'id' => $id,
                    'status' => $status
                ];
                if (!$RoleValidate->scene('up')->check($data)) {
                    return $this->show(config('status.error'), $RoleValidate->getError());
                }

                try {
                    $BusRole = new BusRole();
                    $result = $BusRole::upStatus($data);
                    if ($result !== false) {
                        return $this->show(config('status.success'), '更新成功');
                    } else {
                        return $this->show(config('status.error'), '更新失败');
                    }
                } catch (\Exception $e) {
                    return $this->show(config('status.error'), $e->getMessage());
                }
            } else {
                return $this->show(config('status.error'), '参数错误');
            }

        } else {
            return $this->show(config('status.error'), '请求类型错误');
        }

    }

    /**
     * 功能说明 设置权限
     * @author: wang
     * @Date: 2020/3/27 10:00
     * @return false|string|\think\response\Json
     * @throws \Exception
     */
    public function set()
    {
        if($this->request->isPost()){

            $id = $this->request->param('id', '', 'trim,htmlspecialchars');
            $type = $this->request->param('type', '', 'trim,htmlspecialchars');
            $BusRole = new BusRole();
            //设置个人权限
            if (!empty($type) && $type == 'setAuth') {
                $data = $this->request->param('', '', 'trim,htmlspecialchars');
                try {
                    $result = $BusRole::setRoleAuth($data);
                } catch (\Exception $e) {
                    return $this->show(config('status.error'), $e->getMessage());
                }
                if ($result !== false) {
                    return $this->result(config('status.success'), '设置成功', '/admin/role/index');
                } else {
                    return $this->show(config('status.error'), '数据异常');
                }
            }
            //获取全部权限及已有权限
            try {
                $result = $BusRole::getRoleAuth($id);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }
            if ($result !== false) {
                //权限插件数据特殊 勿改
                $data = [
                    'code' => config('status.success'),
                    'data' => $result
                ];
                return json_encode($data, JSON_UNESCAPED_UNICODE);
            } else {
                return $this->show(config('status.error'), '数据异常');
            }

        }

        return View::fetch('set');
    }


}